package com.hkbigdata.sybx.controller;


import com.hkbigdata.sybx.bean.ServiceEvaluation;
import com.hkbigdata.sybx.service.ServiceEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuanbo
 * @creat 2025-05-31-22:10
 * @see 2194550857@qq.com
 */
@RestController
@RequestMapping("/api/serviceEvaluation")
public class ServiceEvaluationController {
    @Autowired
    private ServiceEvaluationService serviceEvaluationService;
    @PostMapping("/insert")
    public Map<String, Object> serviceEvaluationInsert(@RequestBody ServiceEvaluation serviceEvaluation){
        boolean success = serviceEvaluationService.ServiceEvaluationInert(serviceEvaluation);
        Map<String,Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    @GetMapping("/select")
    public Map<String, Object> serviceEvaluationSelect(@RequestParam("userId") String userId) {
        try {
            Long id = Long.parseLong(userId);
            boolean exists = serviceEvaluationService.ServiceEvaluationSelect(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", exists);
            return response;
        } catch (NumberFormatException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "用户ID格式不正确");
            return response;
        }
    }


    @GetMapping("/SelectEvaluation")
    public List<ServiceEvaluation> serviceEvaluationSelect() {
        return serviceEvaluationService.ServiceEvaluationSelect();
    }
    // 添加删除接口：根据用户ID删除
    @DeleteMapping("/deleteByUserId/{userId}")
    public Map<String, Object> deleteByUserId(@PathVariable("userId") String userId) {
        Map<String, Object> response = new HashMap<>();
        try {
            boolean success = serviceEvaluationService.deleteByUserId(userId);
            response.put("success", success);
            response.put("message", success ? "删除成功" : "删除失败，未找到该用户的评价");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "删除异常：" + e.getMessage());
            e.printStackTrace();
        }
        return response;
    }
}


